package com.handpay.zztong.hp.accountcenter.activity;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.SystemClock;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.handpay.framework.ClientEngine;
import com.handpay.framework.HttpEngine;
import com.handpay.framework.HttpSFTConnector;
import com.handpay.framework.LuaTableUtil;
import com.handpay.framework.SecureManager;
import com.handpay.framework.utils.BitmapLoader;
import com.handpay.framework.utils.CommonUtils;
import com.handpay.framework.utils.MyAsyncTask;
import com.handpay.zztong.hp.R;
import com.handpay.zztong.hp.base.activity.ZZTongActivity;
import com.handpay.zztong.hp.bean.ActionBar;
import com.handpay.zztong.hp.bean.ResponseBean;
import com.handpay.zztong.hp.config.ZZTConfig;
import com.handpay.zztong.hp.config.ZZTConstant;
import com.handpay.zztong.hp.log.HPLog;
import com.handpay.zztong.hp.protocol.BaseProtocolKey;
import com.handpay.zztong.hp.protocol.CardProtocolKey;
import com.handpay.zztong.hp.protocol.UCProtocolKey;
import com.handpay.zztong.hp.protocol.UploadProtocolConstant;
import com.handpay.zztong.hp.swiper.SwiperUtils;
import com.handpay.zztong.hp.ui.CustomProgressDialog;
import com.handpay.zztong.hp.utils.AccountUtils;
import com.handpay.zztong.hp.utils.AccountUtils.AccountStatus;

import java.util.Hashtable;
import java.util.zip.CRC32;

import se.krka.kahlua.vm.LuaTable;

public class BaseInfoConfirmUploadActivity extends ZZTongActivity {
    private static final String TAG = BaseInfoConfirmUploadActivity.class.getSimpleName();
    private String mName;
    private String mCertNo;
    private String mShopName;
    private String mCardNum;

    @Override
    protected void onStart() {
        //在确认界面，再次提醒用户打开地理位置信息
        this.requestCurrentLocation(true);
        super.onStart();
    }

    private String mBankName = null;
    private String mSubBankName = null;
    private String mBankCode = null;
    private String mSubBankCode = null;
    private String mProvinceId = null;
    private String mProvinceName = null;
    private String mAreaId = null;
    private String mAreaName = null;
    private String mFrontUri;
    private volatile boolean mFrontUploaded = false;
    private String mBackUri;
    private volatile boolean mBackUploaded = false;
    private String mInHandUri;
    private volatile boolean mInHandUploaded = false;
    private String mShopUri;
    private volatile boolean mShopUploaded = false;
    private String mBusinnessUri;
    private volatile boolean mBUploaded = false;
    private ImageView open_province_line, open_city_line, open_subbank_line;
    private TextView open_province, open_city, open_subBank;
    private View mProvinceTable, mAreaTable, mSubBankTable;

    private MyAsyncTask<Void, UploadProtocolConstant.TYPE, Integer> mTask;
    //保存用户提交时的csn。
    private String mCsn;
    private boolean isNeedBind;
    CustomProgressDialog progressDialog = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        this.setContentView(R.layout.activity_base_info_confirm_upload);
        super.onCreate(savedInstanceState);
        //当前这个界面，保持该界面不会黑屏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        getData();
        open_province = (TextView) this.findViewById(R.id.tb_opening_province);
        open_city = (TextView) this.findViewById(R.id.tb_opening_city);
        open_subBank = (TextView) this.findViewById(R.id.tb_sub_bank_name);
        mProvinceTable = this.findViewById(R.id.opening_province);
        mAreaTable = this.findViewById(R.id.opening_city);
        mSubBankTable = this.findViewById(R.id.open_sub_bank);

        open_province_line = (ImageView) this.findViewById(R.id.opening_province_line);
        open_city_line = (ImageView) this.findViewById(R.id.opening_city_line);
        open_subbank_line = (ImageView) this.findViewById(R.id.opening_sub_bank_line);
        if (!TextUtils.isEmpty(mProvinceId) && !TextUtils.isEmpty(mProvinceName)) {
            mProvinceTable.setVisibility(View.VISIBLE);
            open_province.setText(mProvinceName);
            open_province_line.setVisibility(View.VISIBLE);
        }
        if (!TextUtils.isEmpty(mAreaId) && !TextUtils.isEmpty(mAreaName)) {
            mAreaTable.setVisibility(View.VISIBLE);
            open_city.setText(mAreaName);
            open_city_line.setVisibility(View.VISIBLE);
        }
        if (!TextUtils.isEmpty(mSubBankCode) && !TextUtils.isEmpty(mSubBankName)) {
            mSubBankTable.setVisibility(View.VISIBLE);
            open_subBank.setText(mSubBankName);
            open_subbank_line.setVisibility(View.VISIBLE);
        }

        if (!isDataReady()) {
            //重新进入该界面的时候，有些uri是不是可以没有？
            HPLog.e(TAG, "miss upload data!");
            finish();
        }
        fillData();
        this.findViewById(R.id.nextButton).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                //最后确认的时候，同样要确保刷卡器是连接的
                if (checkSwiperOk(BaseInfoConfirmUploadActivity.this, true)) {
                    mCsn = getCSN();
                    isNeedBind = SwiperUtils.canBind();
                    String address = (String) ClientEngine.getInstance().getGlobal(BaseProtocolKey.KEY_ADDRESS);
                    if (!TextUtils.isEmpty(address)) {
                        doUpload();
                    } else {
                        getCurrentLocation();
                    }
                }
            }
        });
    }

    @Override
    public ActionBar getActivityActionBar() {
        return new ActionBar(this.getString(R.string.comfirm_compose_base_info_title), true);
    }

    private void getData() {
        Intent t = this.getIntent();
        mName = t.getStringExtra(UCProtocolKey.KEY_NAME);
        mCertNo = t.getStringExtra(UCProtocolKey.KEY_CERTNO);
        mShopName = t.getStringExtra(UCProtocolKey.KEY_SHOPNAME);
        mCardNum = t.getStringExtra(UCProtocolKey.KEY_CARDNUM);
        mBankCode = t.getStringExtra(UCProtocolKey.KEY_BANKCODE);
        mBankName = t.getStringExtra(UCProtocolKey.KEY_BANKNAME);
        mSubBankCode = t.getStringExtra(UCProtocolKey.KEY_SUBBANKCODE);
        mSubBankName = t.getStringExtra(UCProtocolKey.KEY_SUBBANKNAME);
        mFrontUri = t.getStringExtra(UploadProtocolConstant.TYPE.FRONT.toString());
        mBackUri = t.getStringExtra(UploadProtocolConstant.TYPE.BACK.toString());
        mInHandUri = t.getStringExtra(UploadProtocolConstant.TYPE.INHAND.toString());
        mShopUri = t.getStringExtra(UploadProtocolConstant.TYPE.SHOPPIC.toString());
        mBusinnessUri = t.getStringExtra(UploadProtocolConstant.TYPE.BUS_LIC.toString());
        mProvinceId = t.getStringExtra(UCProtocolKey.KEY_PROVIVCEID);
        mProvinceName = t.getStringExtra(UCProtocolKey.KEY_PROVINCENAME);
        mAreaId = t.getStringExtra(UCProtocolKey.KEY_AREAID);
        mAreaName = t.getStringExtra(UCProtocolKey.KEY_AREANAME);
    }

    private void fillData() {
        ((TextView) this.findViewById(R.id.tb_user_name)).setText(mName);
        ((TextView) this.findViewById(R.id.tb_certno)).setText(mCertNo);
        ((TextView) this.findViewById(R.id.tb_shop_name)).setText(mShopName);
        ((TextView) this.findViewById(R.id.tb_card_no)).setText(mCardNum);
        ((TextView) this.findViewById(R.id.tb_main_bank_name)).setText(mBankName);
//		((TextView)this.findViewById(R.id.tb_sub_bank_name)).setText(mSubBankName);
    }

    @Override
    protected boolean isShownTabs() {
        return false;
    }

    private boolean isDataReady() {
        return !TextUtils.isEmpty(mName) &&
                !TextUtils.isEmpty(mCertNo) &&
                !TextUtils.isEmpty(mShopName) &&
                !TextUtils.isEmpty(mCardNum) &&
                !TextUtils.isEmpty(mBankName) &&
                !TextUtils.isEmpty(mBankCode) &&
                !TextUtils.isEmpty(mFrontUri) &&
                !TextUtils.isEmpty(mBackUri) &&
                !TextUtils.isEmpty(mInHandUri) &&
                !TextUtils.isEmpty(mShopUri);
    }

    private void doUpload() {
        HPLog.i("time", "doUpload:" + SystemClock.elapsedRealtime());
        cancelTask();
        //检测网络，如果网络不可用，提示打开网络
        if (checkNetAndAlert(this)) {
            return;
        }
        //分别上传四、五张照片。最后发送提交.如果其中有一个发送失败。提示用户发送失败，请重新提交。
        //返回值。0表示成功，1表示失败，2，表示用户取消
        mTask = new MyAsyncTask<Void, UploadProtocolConstant.TYPE, Integer>() {
            @Override
            protected void onPreExecute() {
                HPLog.i(HPLog.YL_TAG, "onPreExecute:");
                progressDialog = new CustomProgressDialog(BaseInfoConfirmUploadActivity.this, R.style.Theme_CustomDialog);
                progressDialog.setMessage(getString(R.string.uploading_template, ""));//
                progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                progressDialog.setCancelable(false);
                progressDialog.show();
                super.onPreExecute();
            }

            @Override
            protected void onPostExecute(Integer result) {
                HPLog.i(HPLog.YL_TAG, "onPostExecute:");
                if (null != progressDialog) {
                    progressDialog.dismiss();
                    progressDialog = null;
                }
                if (0 == result) {
                    //成功以后，删除图片
                    CommonUtils.removePics();
                    gotoUploadSuccess();
                } else if (1 == result) {
                    //上传失败
                    Toast.makeText(BaseInfoConfirmUploadActivity.this, R.string.upload_failed, Toast.LENGTH_SHORT).show();
                } else if (2 == result) {
                    //上传取消
                    Toast.makeText(BaseInfoConfirmUploadActivity.this, R.string.upload_cancel, Toast.LENGTH_LONG).show();
                    HPLog.e(TAG, "upload canceled");
                }
                super.onPostExecute(result);
            }

            @Override
            protected void onProgressUpdate(UploadProtocolConstant.TYPE... values) {
                if (null != progressDialog) {
                    String picDes = getString(R.string.ID_card_photo);
                    switch (values[0]) {
                        case FRONT: {
                            picDes = getString(R.string.ID_card_photo);
                            break;
                        }
                        case BACK: {
                            picDes = getString(R.string.ID_card_back_photo);
                            break;
                        }
                        case INHAND: {
                            picDes = getString(R.string.ID_person_with_card);
                            break;
                        }
                        case SHOPPIC: {
                            picDes = getString(R.string.shop_pic);
                            break;
                        }
                        case BUS_LIC: {
                            picDes = getString(R.string.business_license);
                            break;
                        }
                    }
                    HPLog.i(HPLog.YL_TAG, "onProgressUpdate 2:" + getString(R.string.uploaded_template, picDes));

                    progressDialog.setMessage(getString(R.string.uploading_template, picDes));
                }
                super.onProgressUpdate(values);
            }

            @Override
            protected void onCancelled(Integer result) {
                HPLog.i(HPLog.YL_TAG, "onCancelled:");
                if (null != progressDialog) {
                    progressDialog.dismiss();
                    progressDialog = null;
                }
                //上传取消
                Toast.makeText(BaseInfoConfirmUploadActivity.this, R.string.upload_cancel, Toast.LENGTH_LONG).show();
                HPLog.e(TAG, "upload canceled");
                super.onCancelled();
            }

            @Override
            protected Integer doInBackground(Void... params) {
                HPLog.i(HPLog.YL_TAG, "doInBackground:");
                CRC32 mCrc32 = new CRC32();
                //发送正面图片
                if (!mFrontUploaded) {
                    if (uploadPhotoJson(mFrontUri, UploadProtocolConstant.TYPE.FRONT, mCrc32)) {
                        this.publishProgress(UploadProtocolConstant.TYPE.FRONT);
                        mFrontUploaded = true;
                    } else {
                        return 1;
                    }
                }
                if (this.isCancelled()) {
                    return 2;
                }
                //发送反面图片
                if (!mBackUploaded) {
                    if (uploadPhotoJson(mBackUri, UploadProtocolConstant.TYPE.BACK, mCrc32)) {
                        this.publishProgress(UploadProtocolConstant.TYPE.BACK);
                        mBackUploaded = true;
                    } else {
                        return 1;
                    }
                }
                if (this.isCancelled()) {
                    return 2;
                }
                //发送手持图片
                if (!mInHandUploaded) {
                    if (uploadPhotoJson(mInHandUri, UploadProtocolConstant.TYPE.INHAND, mCrc32)) {
                        this.publishProgress(UploadProtocolConstant.TYPE.INHAND);
                        mInHandUploaded = true;
                    } else {
                        return 1;
                    }
                }
                if (this.isCancelled()) {
                    return 2;
                }
                //发送商店
                if (!mShopUploaded) {
                    if (uploadPhotoJson(mShopUri, UploadProtocolConstant.TYPE.SHOPPIC, mCrc32)) {
                        this.publishProgress(UploadProtocolConstant.TYPE.SHOPPIC);
                        mShopUploaded = true;
                    } else {
                        return 1;
                    }
                }
                if (this.isCancelled()) {
                    return 2;
                }
                if (!TextUtils.isEmpty(mBusinnessUri)) {
                    //发送营业执照
                    if (!mBUploaded) {
                        if (uploadPhotoJson(mBusinnessUri, UploadProtocolConstant.TYPE.BUS_LIC, mCrc32)) {
                            this.publishProgress(UploadProtocolConstant.TYPE.BUS_LIC);
                            mBUploaded = true;
                        } else {
                            return 1;
                        }
                    }
                }
                //发送基础身份信息
                if (!uploadInfoWithJson(!TextUtils.isEmpty(mBusinnessUri))) {
                    return 1;
                }
                return 0;
            }

        };
        mTask.execute();
    }

    private void gotoUploadSuccess() {
        AccountUtils.setShopName(this, mShopName);
        //提交成功后，将状态设置为审核中
        AccountUtils.setAccountStatus(this, AccountStatus.CHECKING);
        Intent i = new Intent(this, BaseInfoUploadSuccessActivity.class);
        //该csn需要绑定。
        if (isNeedBind) {
            i.putExtra(CardProtocolKey.CSN_KEY, mCsn);
        }
        ZZTongActivity.isUploadComposeSusses = true;
        this.startActivity(i);
        this.finish();
    }

    @Override
    protected void onStop() {
        HPLog.i("ssss", "onStop");
        cancelTask();
        super.onStop();
    }

    private void cancelTask() {
        //当该activity不可见的时候，取消上传任务。
        if (null != mTask) {
            HPLog.i("ssss", "cancel task");
            mTask.cancel(true);
            mTask = null;
        }
    }

    private boolean uploadPhotoJson(String uri, UploadProtocolConstant.TYPE type, CRC32 crc32) {
        HPLog.i(TAG, "upload url:" + uri);
        Hashtable<String, String> p = new Hashtable<String, String>();
        String acc = AccountUtils.getAccountName(this);
        String channel = ClientEngine.CHANNEL;
        int t = type.ordinal();
        String hex = "";
        String imgCheck = "";

        byte[] imgByte = null;
        Bitmap bitmap;
        if (BitmapLoader.isHttpUrl(uri)) {
            HPLog.i(TAG, "uploadPhoto isHttpUrl(uri)");
            imgByte = BitmapLoader.getInstance(BaseInfoConfirmUploadActivity.this).loadBitmapNoCompress(uri);
        } else {
            HPLog.i(TAG, "uploadPhoto isNotHttpUrl(uri)");
            bitmap = BitmapLoader.getInstance(BaseInfoConfirmUploadActivity.this).loadBitmap(uri, uri);
            imgByte = CommonUtils.getBitmapBytes(bitmap, ZZTConstant.MAXBYTE);
        }

        HPLog.i(TAG, "  CommonUtils.getBitmapBytes after" + type + " size: " + imgByte.length / 1024);
        imgCheck = CommonUtils.getCRC(crc32, imgByte);
        p.put(UploadProtocolConstant.KEY_CHECK, imgCheck);
        p.put(UploadProtocolConstant.ACCOUNT, SecureManager.getInstance().des(acc, 1, null));
        p.put(UploadProtocolConstant.CHANNEL, channel);
        p.put(UploadProtocolConstant.KEY_PICTYPE, String.valueOf(t));
        // 根据后台的情况，采用不同的上传方式。目前采用上传HEX str.
        // 等待后期后台，可以接受二进制上传。再采用上传二进制的方式。二进制上传包会小一倍。
        ResponseBean responseBean;
        if (ZZTConfig.UPLOAD_BIN) {
            // 传二进制数据到后台
            HPLog.i("time", "uploadPhoto: after prepare data" + SystemClock.elapsedRealtime() + "" + " " + type.toString());
            responseBean = HttpSFTConnector.uploadPic2ServerWithJson(BaseInfoConfirmUploadActivity.this, ResponseBean.class, Do_UploadPicInfo, p, imgByte);
            HPLog.i("time", "uploadPhoto: after response data" + SystemClock.elapsedRealtime() +
                    " " + type.toString());
        } else {
            // 传HEX str到后台
            hex = CommonUtils.bytesToHexString(imgByte);
            if (TextUtils.isEmpty(hex)) {
                // outOfMemory 返回上传失败。
                return false;
            }
            p.put(UploadProtocolConstant.KEY_PIC, hex);
            HPLog.i("time", "uploadPhoto: after prepare data" + SystemClock.elapsedRealtime() + " " + type.toString());
            responseBean = HttpSFTConnector.postData2ServerWithJson(BaseInfoConfirmUploadActivity.this, ResponseBean.class, Do_UploadPicInfo, p);
            HPLog.i("time", "uploadPhoto: after response data" + SystemClock.elapsedRealtime() + " " + type.toString());
        }
        if (!TextUtils.isEmpty((responseBean.getErrMessage())) && !TextUtils.isEmpty(responseBean.getResponseCode())) {
            if ("99".equals(responseBean.getResponseCode())) {
                this.showAlertDialog(this, getString(R.string.prompt), responseBean.getErrMessage(), false, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        exitAccount();
                        dialog.dismiss();
                    }
                });
                return false;
            } else if ("999".equals(responseBean.getResponseCode())) {
                showAlertDialog(this, getString(R.string.offline_notify_txt), responseBean.getErrMessage(), false, getString(R.string.tv_exit), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        exitapp();
                        dialog.dismiss();
                    }
                }, getString(R.string.tv_relogin), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        exitAccount();
                        dialog.dismiss();
                    }
                });
                return false;
            }
            return false;
        }
        return true;
    }

    private boolean uploadPhoto(String uri, UploadProtocolConstant.TYPE type, CRC32 crc32) {
        HPLog.i(TAG, "upload url:" + uri);
        Hashtable<String, String> p = new Hashtable<String, String>();
        String acc = AccountUtils.getAccountName(this);
        String channel = ClientEngine.CHANNEL;
        int t = type.ordinal();
        String hex = "";
        String imgCheck = "";

        byte[] imgByte = null;
        Bitmap bitmap;
        if (BitmapLoader.isHttpUrl(uri)) {
            HPLog.i(TAG, "uploadPhoto isHttpUrl(uri)");
            imgByte = BitmapLoader.getInstance(BaseInfoConfirmUploadActivity.this).loadBitmapNoCompress(uri);
        } else {
            HPLog.i(TAG, "uploadPhoto isNotHttpUrl(uri)");
            bitmap = BitmapLoader.getInstance(BaseInfoConfirmUploadActivity.this).loadBitmap(uri, uri);
            imgByte = CommonUtils.getBitmapBytes(bitmap, ZZTConstant.MAXBYTE);
        }

        HPLog.i(TAG, "  CommonUtils.getBitmapBytes after" + type + " size: " + imgByte.length / 1024);
        imgCheck = CommonUtils.getCRC(crc32, imgByte);
        p.put(UploadProtocolConstant.KEY_CHECK, imgCheck);
        p.put(UploadProtocolConstant.ACCOUNT, SecureManager.getInstance().des(acc, 1, null));
        p.put(UploadProtocolConstant.CHANNEL, channel);
        p.put(UploadProtocolConstant.KEY_PICTYPE, String.valueOf(t));
        Hashtable<String, Object> resp;
        // 根据后台的情况，采用不同的上传方式。目前采用上传HEX str.
        // 等待后期后台，可以接受二进制上传。再采用上传二进制的方式。二进制上传包会小一倍。
        if (ZZTConfig.UPLOAD_BIN) {
            // 传二进制数据到后台
            HPLog.i("time", "uploadPhoto: after prepare data" + SystemClock.elapsedRealtime() + "" + " " + type.toString());
            resp = HttpSFTConnector.uploadPic2Server(BaseInfoConfirmUploadActivity.this, Do_UploadPicInfo, p, imgByte);
            HPLog.i("time", "uploadPhoto: after response data" + SystemClock.elapsedRealtime() +
                    " " + type.toString());
        } else {
            // 传HEX str到后台
            hex = CommonUtils.bytesToHexString(imgByte);
            if (TextUtils.isEmpty(hex)) {
                // outOfMemory 返回上传失败。
                return false;
            }
            p.put(UploadProtocolConstant.KEY_PIC, hex);
            HPLog.i("time", "uploadPhoto: after prepare data" + SystemClock.elapsedRealtime() + " " + type.toString());
            resp = HttpSFTConnector.postData2Server(BaseInfoConfirmUploadActivity.this, Do_UploadPicInfo, p);
            HPLog.i("time", "uploadPhoto: after response data" + SystemClock.elapsedRealtime() + " " + type.toString());
        }

        int respCode = (Integer) resp.get(HttpEngine.Key_RespCode);
        String errMsg = (String) resp.get(HttpEngine.Key_RespErrMsg);
        if (respCode < 0 || !TextUtils.isEmpty(errMsg)) {
            return false;
        }
        byte data[] = (byte[]) resp.get(HttpEngine.Key_RespData);
        if (data == null) {
            return false;
        }
        LuaTable luaTable = LuaTableUtil.stringToLuaTable(data, false);
        if (luaTable == null) {
            return false;
        }
        String em = (String) luaTable.rawget("errMessage");
        if (!TextUtils.isEmpty(em)) {
            dealWithLoginTimeOut(resp);
            return false;
        }
        return true;
    }




    /**
     * 发送数据到后台，请求验证
     *
     * @return 是否检查服务器数据成功
     */
    private boolean uploadInfoWithJson(boolean isSendBusinessPic) {
        HPLog.i("time", "uploadInfo: before prepare data" + SystemClock.elapsedRealtime());
        Hashtable<String, String> p = new Hashtable<String, String>();
        String phone = AccountUtils.getAccountPhone(this);
        String name = mName;
        String certNo = mCertNo;
        String shopName = mShopName;
        String cardNo = mCardNum;
        String bankCode = mBankCode;
        String subBankCode = mSubBankCode;
        String channel = ClientEngine.CHANNEL;
        String type = UCProtocolKey.TYPE_UPLOAD;
        p.put(UCProtocolKey.PHONE, SecureManager.getInstance().des(phone, 1, null));
        p.put(UCProtocolKey.KEY_NAME, SecureManager.getInstance().des(name, 1, null));
        p.put(UCProtocolKey.KEY_CERTNO, SecureManager.getInstance().des(certNo, 1, null));
        p.put(UCProtocolKey.KEY_SHOPNAME, SecureManager.getInstance().des(shopName, 1, null));
        p.put(UCProtocolKey.KEY_CARDNUM, SecureManager.getInstance().des(cardNo, 1, null));
        p.put(UCProtocolKey.KEY_BANKCODE, bankCode);
        p.put(UCProtocolKey.CHANNEL, channel);
        p.put(UCProtocolKey.KEY_TYPE, type);
        if (!TextUtils.isEmpty(mProvinceId)) {
            p.put(UCProtocolKey.KEY_PROVIVCEID, String.valueOf(mProvinceId));
        }
        if (!TextUtils.isEmpty(mAreaId)) {
            p.put(UCProtocolKey.KEY_AREAID, String.valueOf(mAreaId));
        }
        if (!TextUtils.isEmpty(subBankCode)) {
            p.put(UCProtocolKey.KEY_SUBBANKCODE, subBankCode);
        }
        String imageStyle;
        if (!isSendBusinessPic) {
            imageStyle = String.valueOf(UploadProtocolConstant.TYPE.BUS_LIC.ordinal());
        } else {
            imageStyle = "";
        }
        //不发送的照片类型。
        p.put(UCProtocolKey.KEY_IMAGE_TYPE, imageStyle);
        //使用点击确认时候，绑定刷卡器的csn。
        p.put(CardProtocolKey.CSN_KEY, SecureManager.getInstance().des(mCsn, 1, null));
        this.appendAddresses(p, true);
        p.put(BaseProtocolKey.NEWBANKFLAG, newBankFlag);

//        String HEADER_NAME_TerminalUserId = "X-HPTerminalUserID";
//        ClientEngine.getInstance().saveGlobal(HEADER_NAME_TerminalUserId,null);
//        String terminalUserId = (String) ClientEngine.getInstance().getGlobal(HEADER_NAME_TerminalUserId);
//        HPLog.i(HPLog.YL_TAG,"HEADER_NAME_TerminalUserId:"+terminalUserId);

        ResponseBean responseBean = HttpSFTConnector.postData2ServerWithJson(BaseInfoConfirmUploadActivity.this, ResponseBean.class, Do_UploadCheckInfo, p);
        if (responseBean.getResponseCode() != null && responseBean.getErrMessage() != null) {
            this.showAlertDialog(this, getString(R.string.prompt), responseBean.getErrMessage(), false);
            return false;
        } else {
            return true;
        }
    }

//    @Override
//    protected void onDestroy() {
//        if (progressDialog != null) {
//            progressDialog.dismiss();
//            progressDialog = null;
//        }
//        super.onDestroy();
//    }
}
